package cn.itzzx.lagou.mybatis.binding;

import cn.itzzx.lagou.mybatis.session.SqlSession;

import java.lang.reflect.Proxy;

/**
 * 该类负责创建动态代理对象
 *
 * @author Martin
 * @since 1.0
 */
public class MapperProxyFactory<T> {

    /**
     * 被代理的接口
     */
    private final Class<T> mapperInterface;

    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public T newInstance(SqlSession sqlSession) {
        //创建了JDK动态代理的invocationHandler接口的实现类mapperProxy
        MapperProxy <T> mapperProxy = new MapperProxy<T>(sqlSession);
        //调用重载方法
        return newInstance(mapperProxy);
    }

    protected T newInstance(MapperProxy<T> mapperProxy) {
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }
}

